package com.macllvm.whale.ui;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import java.lang.Override;
import java.util.Observer;
import java.util.Observable;
import com.macllvm.whale.model.BuddyManager;
import com.macllvm.whale.entity.Buddy;
import com.macllvm.whale.adapter.BuddyAdapter;
import com.macllvm.whale.protocol.ProtocolController;
import com.macllvm.whale.Event;
import com.macllvm.whale.R;


public class BuddysFragment extends Fragment implements Observer {

    private ListView mBuddyListView;
    private Context mContext;
    private BuddyAdapter mBuddyAdapter;
    private ProtocolController mController;
    private View mView;

    @Override
    public void onAttach (Activity activity) {
        super.onAttach(activity);
        mContext = (Context)activity;
        mBuddyAdapter = new BuddyAdapter(mContext);
        mController = ProtocolController.getInstance();
        mBuddyAdapter.setDataSource(mController.getBuddyManager().getDataSource());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)  	{
        if(null == mView) {
            mView = inflater.inflate(R.layout.buddys_fragment, container, false);
            mBuddyListView = (ListView) mView.findViewById(R.id.buddy_list);
            mBuddyListView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Buddy b = (Buddy) (mController.getBuddyManager().get(position));
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), ChatActivity.class);
                    intent.putExtra("ip", b.getIP());
                    startActivity(intent);
                }
            });

        }else{
            ViewGroup parent = (ViewGroup)mView.getParent();
            if (null != parent) {
                parent.removeView(mView);
            }
        }

        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mBuddyListView.setAdapter(mBuddyAdapter);
    }

    @Override
    public void onResume() {
        super.onResume();
        handler.sendEmptyMessage(Event.EVENT_REFRESH_BUDDY);
    }

    @Override
    public void onStart() {
        super.onStart();
        mController.getBuddyManager().addObserver(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        mController.getBuddyManager().deleteObserver(this);
    }

    @Override
    public void update(Observable object, Object argument) {
        handler.sendEmptyMessage(Event.EVENT_REFRESH_BUDDY);
    }

    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
        switch (msg.what) {
            case Event.EVENT_REFRESH_BUDDY:
                mBuddyAdapter.notifyDataSetChanged();
                break;
        }
        }
    };

}
